<template>
  <div>
    <backContainer>
      <card class="infobox" :top="20" style="margin:20px auto 0 auto;">
        <div class="title">修改密码</div>
        <div class="formbox pageForm">
          <el-form @submit.native.prevent ref="form" :model="user" :rules="rules" label-width="120px">
            <el-form-item label="旧密码" prop="oldPassword">
              <el-input autocomplete="off" style="width:320px;" v-model="user.oldPassword" placeholder="请输入旧密码"
                        type="password" show-password
              />
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input style="width:320px;" v-model="user.newPassword" placeholder="请输入新密码" type="password"
                        show-password
              />
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input style="width:320px;" v-model="user.confirmPassword" placeholder="请确认新密码"
                        type="password" show-password
              />
            </el-form-item>
            <div class="flex" style="margin: 40px 0 0 120px;">
              <el-button @click="cancel">取消</el-button>
              <el-button style="margin-left:32px ;" type="primary" @click="submit">确认</el-button>
            </div>
          </el-form>
        </div>

      </card>
    </backContainer>

  </div>
</template>

<script>
import { updateUserPwd } from '@/api/system/user'

export default {
  name: 'Profile',
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      // 验证码是否CD
      codeCd: false,
      // CD长度
      long: 60,
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: '旧密码不能为空', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '新密码不能为空', trigger: 'blur' },
          {
            pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{6,20}$/,
            message: '密码由6-20位数字、字母、符号三种组成',
            trigger: 'blur'
          }
        ],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
  },
  methods: {
    // 获取手机验证码
    handleCaptcha() {
      if (!this.codeCd) {
        // 开启计时
        this.codeCd = true
        const timer = setInterval(() => {
          this.long--
          if (this.long <= 0) {
            this.long = 60
            this.codeCd = false
            clearInterval(timer)
          }
        }, 1000)
      }
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '正在处理…',
            spinner: 'el-icon-loading',
            background: 'rgba(255, 255, 255, 0.6)'
          })
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(() => {
            loading.close()
            this.$modal.msgSuccess('修改成功')
            setTimeout(() => {
              this.$router.go(-1)
            }, 1000)
          }).catch(() => {
            loading.close()
          })
        }
      })
    },
    cancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss" scoped>
.infobox {
  .title {
    padding: 32px 0 0 40px;
    font-size: 18px;
    font-family: PingFangSC-Semibold, PingFang SC, sans-serif;
    font-weight: 600;
    color: #333333;
    line-height: 25px;
  }

  .formbox {
    padding: 41px 0 0 211px;
  }

  .avatarTip {
    margin-left: 10px;
    height: 80px;
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC, sans-serif;
    font-weight: 400;
    color: #A3A3A3;
    line-height: 140px;
  }

  .reset {
    margin-left: 18px;
    font-size: 16px;
    font-family: PingFangSC-Regular, PingFang SC, sans-serif;
    font-weight: 400;
    color: #28B28B;
  }
}
</style>
